import tkinter as tk
import random

# 定义扫雷游戏类
class Minesweeper:
    def __init__(self, root, rows, cols, mines):
        self.root = root
        self.root.title("扫雷游戏")
        self.rows = rows
        self.cols = cols
        self.mines = mines
        self.board = [[0 for _ in range(cols)] for _ in range(rows)]
        self.revealed = [[False for _ in range(cols)] for _ in range(rows)]
        self.buttons = [[None for _ in range(cols)] for _ in range(rows)]
        self.game_over = False
        self.setup_board()
        self.create_widgets()

    def setup_board(self):
        # 随机放置地雷
        mine_count = 0
        while mine_count < self.mines:
            x = random.randint(0, self.rows - 1)
            y = random.randint(0, self.cols - 1)
            if self.board[x][y] != -1:
                self.board[x][y] = -1
                mine_count += 1

        # 计算每个格子周围的地雷数量
        for i in range(self.rows):
            for j in range(self.cols):
                if self.board[i][j] != -1:
                    count = 0
                    for dx in [-1, 0, 1]:
                        for dy in [-1, 0, 1]:
                            if dx == 0 and dy == 0:
                                continue
                            ni, nj = i + dx, j + dy
                            if 0 <= ni < self.rows and 0 <= nj < self.cols and self.board[ni][nj] == -1:
                                count += 1
                    self.board[i][j] = count

    def create_widgets(self):
        for i in range(self.rows):
            for j in range(self.cols):
                button = tk.Button(self.root, width=2, height=1, command=lambda r=i, c=j: self.click(r, c))
                button.grid(row=i, column=j)
                self.buttons[i][j] = button

    def click(self, row, col):
        if self.game_over or self.revealed[row][col]:
            return
        if self.board[row][col] == -1:
            self.game_over = True
            self.show_all_mines()
            tk.messagebox.showinfo("游戏结束", "你踩到地雷了！")
        else:
            self.reveal(row, col)
            if self.check_win():
                self.game_over = True
                tk.messagebox.showinfo("游戏胜利", "恭喜你，成功排除所有地雷！")

    def reveal(self, row, col):
        if self.revealed[row][col]:
            return
        self.revealed[row][col] = True
        if self.board[row][col] == 0:
            self.buttons[row][col].config(text="", state=tk.DISABLED)
            for dx in [-1, 0, 1]:
                for dy in [-1, 0, 1]:
                    if dx == 0 and dy == 0:
                        continue
                    ni, nj = row + dx, col + dy
                    if 0 <= ni < self.rows and 0 <= nj < self.cols:
                        self.reveal(ni, nj)
        else:
            self.buttons[row][col].config(text=str(self.board[row][col]), state=tk.DISABLED)

    def show_all_mines(self):
        for i in range(self.rows):
            for j in range(self.cols):
                if self.board[i][j] == -1:
                    self.buttons[i][j].config(text="*", state=tk.DISABLED)

    def check_win(self):
        for i in range(self.rows):
            for j in range(self.cols):
                if self.board[i][j] != -1 and not self.revealed[i][j]:
                    return False
        return True


if __name__ == "__main__":
    root = tk.Tk()
    game = Minesweeper(root, rows=8, cols=8, mines=10)
    root.mainloop()